package com.klm.datakeeper;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.dropbox.sync.android.DbxAccountManager;
import com.dropbox.sync.android.DbxFileSystem;

public class MainActivity extends Activity {

	private Button dropboxButton;
	private DbxAccountManager dbxAccountManager;
	private DbxFileSystem dbxFileSystem;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	
		dbxAccountManager = DbxAccountManager.getInstance(getApplicationContext(), DbxApiAppConstants.APP_KEY, DbxApiAppConstants.APP_SECRET);
		if (!dbxAccountManager.hasLinkedAccount()) {
			setContentView(R.layout.activity_main);

			dropboxButton = (Button) findViewById(R.id.dropbox_auth_button);
			dropboxButton.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					dbxAccountManager.startLink(MainActivity.this, DbxApiAppConstants.REQUEST_LINK_TO_DBX);
//					if (!dbxAccountManager.hasLinkedAccount())
//						onClickLinkToDropbox();
//					else {
//						try {
//							dbxFileSystem = DbxFileSystem.forAccount(dbxAccountManager.getLinkedAccount());
//							DbxFile testFile = dbxFileSystem.open(new DbxPath("/hello.txt"));
//							try {
//								// FileInputStream fileInputStream =
//								// testFile.getReadStream();
//								// FileOutputStream fileOutputStream = new
//								// FileOutputStream("");
//								// testFile.writeString("Hello Dropbox!");
//								Toast.makeText(getApplicationContext(),
//										testFile.readString(),
//										Toast.LENGTH_LONG).show();
//							} catch (IOException e) {
//								// TODO Auto-generated catch block
//								e.printStackTrace();
//							} finally {
//								testFile.close();
//							}
//						} catch (Unauthorized e) {
//							e.printStackTrace();
//						} catch (InvalidPathException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						} catch (DbxException e) {
//							// TODO Auto-generated catch block
//							e.printStackTrace();
//						}
//
//					}
				}
			});
		} else {
			Intent intent = new Intent(getApplicationContext(), StartActivity.class);
            startActivity(intent);
		}
		
	}
	
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
	    if (requestCode == DbxApiAppConstants.REQUEST_LINK_TO_DBX) {
	        if (resultCode == Activity.RESULT_OK) {
	            Intent intent = new Intent(getApplicationContext(), StartActivity.class);
	            startActivity(intent);
	        } else {
	            Toast.makeText(getApplicationContext(), "Please, sing in to Dropbox.", Toast.LENGTH_LONG).show();
	        }            
	    } else {
	        super.onActivityResult(requestCode, resultCode, data);
	    }
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
